Ext.define('jwawa.erp.cfg.entity.EntityHeaderListHandler', {
    extend: 'jwawa.erp.framework.ShellHandler',
    entities: ['cfg_entity_headers', 'cfg_entity_details'],
    resources:[
        {
            name: 'entityHeaderStore',
            type: 'store',
            res: 'Ext.data.Store',
            cfg: {
                fields: [
                    {name: 'id',  type: 'int'},
                    {name: 'entity_name',  type: 'string'},
                    'entity_code'
                ],
                proxy:{
                    type: 'jwawaProxy',
                    url : '/entity/find-entity-header-list',
                    reader: {
                        type: 'json',
                        root: 'data'
                    }
                },
                pageSize: DEFAULT_PAGE_SIZE,
                autoLoad: false
            }
        }
    ],
    ref:{
        searchPanel: 'searchPanel',
        entityHeaderGridPanel:'#entityHeaderGridPanel',
        entityPanel: '#entityPanel',

        entityHeaderQueryPanel: '#entityHeaderQueryPanel',
        editEntityButton: '#editEntityButton',
        newEntityButton: '#newEntityButton',
        delEntityButton: '#delEntityButton',
        pagingtoolbar: '#pagingtoolbar'
    },

    bindHandler: function(){
        "use strict";
        var me = this,
            entityHeaderStore = Ext.JwawaResMgr.get('entityHeaderStore'),
            entityHeaderSm = me.entityHeaderGridPanel.getSelectionModel();;

        me.entityHeaderGridPanel.reconfigure(entityHeaderStore);
        me.searchPanel.on('search', me.onSearchEntity, me);
        me.entityHeaderQueryPanel.addListener('activate', me.onActiveEntityHeaderQueryPanel, me);
        me.pagingtoolbar.bind(entityHeaderStore);
        entityHeaderStore.addListener('beforeload', me.onBeforeEntityHeaderStoreLoad, me);
        entityHeaderStore.addListener('load', me.onEntityHeaderStoreLoad, me);
        entityHeaderSm.on('selectionchange', me.onSelectChangeEntityHeader, me);
        me.editEntityButton.on('click', me.onClickEditEntityButton, this);
        me.newEntityButton.on('click', me.onClickNewEntityButton, this);
        me.delEntityButton.on('click', me.onClickDelEntityButton, this);
        me.entityHeaderGridPanel.on('itemdblclick', me.onItemDbClickEntity, this);

        me.view.setPageState('init');
    },

    onClickEditEntityButton: function(){
        "use strict";
        var me = this,
            sm = me.entityHeaderGridPanel.getSelectionModel(),
            record = sm.getSelection()[0];

        var entityId = record.get('id');
        me.editEntity(entityId);
    },

    editEntity: function(entityId){
        "use strict";
        var me = this;

        me.entityPanel.handler.editContent(entityId);
    },

    onClickNewEntityButton: function(){},
    onClickDelEntityButton: function(){},
    onItemDbClickEntity: function(){},

    onActiveEntityHeaderQueryPanel: function(){
        "use strict";
        var me = this,
            sm = me.entityHeaderGridPanel.getSelectionModel();

        // fix select missing after ajax requested
        sm.select(sm.getSelection());
    },

    onEntityHeaderStoreLoad: function(){
        var me = this;

        me.view.setLoading(false);
        return true;
    },

    onBeforeEntityHeaderStoreLoad: function(){
        var me = this;

        me.view.setLoading(true);
    },

    onSelectChangeEntityHeader: function(obj, selected, eOpts){
        "use strict";
        var me = this;

        me.view.setPageState('deselect');
        if(selected.length > 0){
            me.view.setPageState('select');
        }
    },

    onSearchEntity: function(spanel, searchData){
        "use strict";
        var me = this,
            entityHeaderStore = Ext.JwawaResMgr.get('entityHeaderStore'),
            page = 1;

        entityHeaderStore.getProxy().extraParams = searchData;
        entityHeaderStore.loadPage(page);
    },

    onPageStateChange: function(obj, newState, oldState){
        "use strict";
        var me = this;

        switch(newState){
            case 'init':
                me.editEntityButton.disable();
                me.newEntityButton.enable();
                me.delEntityButton.disable();
                break;
            case 'select':
                me.editEntityButton.enable();
                me.newEntityButton.enable();
                me.delEntityButton.enable();
                break;
            case 'deselect':
                me.editEntityButton.disable();
                me.newEntityButton.enable();
                me.delEntityButton.disable();
                break;
        }
    }
});